﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace IMCOM.Imagiality.ImageProcessing.Canvas2D.ROI
{
    public class ROIRectangleScaling : ROIRectangleDrawn
    {
        private RectangleROIHandle activeHandle;
        Point previousMousePosition;

        public new Rectangle ROIRect
        {
            get
            {
                return roiRect;
            }
            set
            {
                roiRect = value;

                // possible performance/memory issue -- everytime a resize is performed, handles are re-created.
                createHandles();
            }
        }

        public ROIRectangleScaling(Canvas2DImageProcessor controller, RectangleROIHandle activeHandle)
            : base(controller)
        {
            this.activeHandle = activeHandle;
        }

        public override void MouseDown(object sender, MouseEventArgs e)
        {
        }

        public override void MouseMoved(object sender, MouseEventArgs e)
        {
            if (!previousMousePosition.IsEmpty)
            {
                Point mouseDelta = new Point(e.X - previousMousePosition.X, e.Y - previousMousePosition.Y);
                ROIRect = activeHandle.Resize(mouseDelta, ROIRect);
                controller.RefreshCanvas();
            }

            // set previous mouse position
            previousMousePosition = new Point(e.X, e.Y);
        }

        public override void MouseUp(object sender, MouseEventArgs e)
        {
            ROIRectangleDrawn newState = new ROIRectangleDrawn(controller);
            newState.ROIRect = roiRect;
            SetControllerState(newState);
        }
    }
}
